/** 发送消息给内容脚本 */
export const sendMessage = (message: string, data: any) => {
  // console.log(window)
  // 创建 自定义 vue-event 事件
  const event = new CustomEvent("vue-event", {
    detail: { message, data }, // 传输数据
    bubbles: true, // 允许冒泡
    composed: true, // 穿透 Shadow DOM
  });
  // 全局触发事件
  window.dispatchEvent(event);
};

/** 监听内容脚本发送的消息 */
export const onMessage = (callback: (e:Event & {detail: {message: string, data: any}})=>void) => {
  window.addEventListener("content-script-event", (e) => {
    let temp = e as Event & {detail: {message: string, data: any}}
    callback(temp);
  });
};
